<?php

// app/Controller/UsersController.php
class UsersController extends AppController {

    var $uses = array(
        'User'
    );

    # Set the layout to use
    var $layout = 'login_layout';

    public function beforeFilter($moduleName = null) {
        parent::beforeFilter();
        $this->Auth->allow('login', 'logout', 'add');
    }

    public function index() {
        $this->redirect(array('controller' => 'Dashboard', 'action' => 'index'));
    }


    public function login() {
        if($this->Auth->user()){
            $this->redirect(array('controller'=>'Dashboard','action'=>'index'));
        }
        $success = true;
        if( $this->request->is('post') ) {
            if( $this->Auth->login() ) {
                /** Check if account is disabled */
                $active = $this->Auth->user('active');
                if( $active == 'n' ) { /** User is disable, Logout */
                    $success = false;
                    $this->Auth->logout();
                    $this->Session->setFlash(__('Your Account is Disabled.'));
                }
            } else {
                $success = false;
                $this->Session->setFlash(__('Invalid login Credentials'));
            }

            if ($success) { //Login Successful
                //Start to store the user id in the cookie for 1 day, let set cookie higher so we have less issue of users
               // $this->Cookie->write('c_user_id',  $this->Auth->user('id'),false,3600 * 24);
                $this->redirect(array('controller' => 'Dashboard', 'action' => 'index'));
            }
        }
    }



    public function logout() {
        $this->Session->destroy();
        $this->redirect($this->Auth->logout());
    }

    /**
     * This function is for making some changes to the user profile
     * @name profile
     * @param void
     * @return void
     * @access public
     */
    public function profile() {
        $this->layout = 'default';

        if ($this->request->is('post')) {
            $this->User->id = $this->Auth->user('id'); // on edit
            $user_save = array('User' => $this->request->data['User']);

            if (!empty($this->request->data['User']['password'])) {
                $user_save['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
            } else {
                unset($user_save['User']['password']);
            }

            $res = $this->User->save($user_save);

            if ($res){
                $user = $this->User->find('first', array(
                    'conditions' => array('User.id' => $this->Auth->user('id')),
                    'recursive' => -1
                ));
                $user = $user['User'];
                $this->Auth->login($user);
                $this->Session->setFlash('Profile info has been updated !');
                $this->Session->write('process_error', 'no');
            } else {
                $this->Session->setFlash('Sorry, Profile info could not be updated');
                $this->Session->write('process_error', 'yes');
            }

            $this->redirect(array('action' => 'profile'));
        }

        $user_info = $this->User->getUserById($this->Auth->user('id'));
        //debug($user_info);

        $controller = $this;
        $title_for_layout = 'Profile';
        $page_description = 'Update Your User Information';
        $this->set(compact('title_for_layout','page_description','controller','user_info'));
    }

    /**
     * This function redirects the user to his appropriate page after login
     * @name signIn
     * @param void
     * @return void
     * @access public
     */
    function action_after_login() {
        $this->redirect(array('controller' => 'Dashboard', 'action' => 'index'));
    }

    public function add() {
        if( $this->request->is('post') ) {
            echo debug($this->request->data);
            $this->User->create();
            if( $this->User->save($this->request->data) ) {
                echo 'The user has been saved';
                $this->redirect(array('action' => 'index'));
            } else {
                echo 'The user could not be saved. Please, try again.';
            }
        }
    }

}